<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Models\GoodsCart;
use App\Models\GoodsSku;
use App\Services\GoodsCartService;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsCartController extends  AdminController
{
    //保存
    public function add(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'sku_id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'num' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method', '_token']);
        $sku_id = $request->input('sku_id');
        $users_id = $request->input('users_id');
        $skuInfo = GoodsService::getInstance()->getSkuInfo($sku_id);
        if(!$skuInfo){
            return $this->fail(CodeResponse::GOODS_CART_INVALID);
        }
        $putData['title'] = $skuInfo->title;
        $putData['image'] = $skuInfo->image;
        $goodsCart = GoodsCartService::getInstance()->goodsCart($sku_id,$users_id);
        $goodsCartId = null;
        if($goodsCart){
            $goodsCartId = $goodsCart->id;
            $putData['num'] = $request->input('num')+$goodsCart->num;
        }

        if($skuInfo->stock < $putData['num']){
            return $this->fail(CodeResponse::GOODS_CART_STOCK);
        }

        $flight = GoodsCart::updateOrCreate(
            ['id' => $goodsCartId],
            $putData
        );
        return $this->success($flight->id);
    }
    public function list(Request $request){
        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $users_id = $request->input('users_id');
        $ids= $request->input('ids');
        $ids = is_array($ids) ? $ids : json_decode($ids,true);
        $goodsCart = GoodsCartService::getInstance()->list($users_id,$ids);

        if(count($goodsCart) > 0 && $goodsCart[0] === false){
            return $this->fail(CodeResponse::GOODS_CART_STOCK,$goodsCart[1]);
        }
        return $this->success($goodsCart);
    }
    public function update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
            'num' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        if($request->num < 1){
            return $this->fail(CodeResponse::GOODS_CART_NUM);
        }

        $goodsCart = GoodsCart::find($request->id);

        if(!$goodsCart){
            return $this->fail(CodeResponse::GOODS_CART_NO);
        }
        $goodsSku = GoodsSku::where('id',$goodsCart->sku_id)->first();
        if($goodsSku->stock < $request->num){
            return $this->fail(CodeResponse::GOODS_CART_STOCK);
        }
        $goodsCart->num = $request->num;
        $goodsCart->save();
        return $this->success($goodsCart->id);
    }
    public function delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goodsCart = GoodsCart::find($request->id);
        $goodsCart->delete();
        return $this->success();
    }
}
